package com.service;

import com.controller.CartController;
import com.entity.Cart;
import com.entity.Commodity;
import com.entity.Reply;
import com.mapper.CartMapper;
import com.mapper.CommodityMapper;
import com.mapper.ReplyMapper;
import com.vo.CartVO;
import jnr.ffi.annotations.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 评论回复 服务类
 * </p>
 *
 * @author hlt
 * @since 2019-12-21
 */
@Service
@Transactional
public class CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CommodityService commodityService;

    //删除购物车
    public int deleteByPrimaryKey(Integer cId) {
        redisTemplate.boundHashOps(CartController.CART_KEY).delete(selectByPrimaryKey(cId).getPId());
        return cartMapper.deleteByPrimaryKey(cId);
    }

    //添加购物车
    public int insert(Cart record) {
        //不管更新还是插入都要删除缓存
        //redisTemplate.boundHashOps(CartController.CART_KEY).delete();
        redisTemplate.delete(CartController.CART_KEY);
        //添加购物车前需要判断缓存中是否存在
        //存在，就是更新，更新前需要删除缓存中购物车
        if (cartMapper.queryCartbyPid(record.getPId()) != null) {
            Integer cId = cartMapper.queryCartbyPid(record.getPId()).getCId();
            record.setCId(cId);
            record.setCNum(record.getCNum() + 1);
            return cartMapper.updateByPrimaryKeySelective(record);
        }
        //那就是插入
        return cartMapper.insert(record);
    }

    public List<CartVO> queryAllCart(Integer page, Integer count, String cId, String key) {
        List<CartVO> list;
        //进来先判断缓存中是否有数据
        List<CartVO> cartlist = redisTemplate.boundHashOps(key).values();

        if (cartlist.isEmpty()) {
            //先读出来，
            List<Cart> carts = cartMapper.queryAllCart(page, count, cId);
            list = new ArrayList<>();
            for (Cart cart : carts) {
                String pId = cart.getPId();
                Commodity commodity = commodityService.LookCommodity(new Commodity().setCommid(pId).setCommstatus(1));
                BigDecimal thinkmoney = commodity.getThinkmoney();
                String commname = commodity.getCommname();
                Integer a = cart.getCNum();
                BigDecimal counts = thinkmoney.multiply(new BigDecimal(a));
                CartVO cartVO = new CartVO(cart.getCId(), commname, thinkmoney, counts, cart.getCNum(), pId);
                list.add(cartVO);
            }
            //在放入缓存
            //两种方案
            //1.用list  太慢；淘汰
            //2.用hash，不能一次性把所有数据存入，需要循环
            for (CartVO cartVO : list) {
                redisTemplate.boundHashOps(key).put(cartVO.getCommid(), cartVO);
            }
        } else {
            list = cartlist;
        }
        return list;
    }


    //查询购物车
    public Cart selectByPrimaryKey(Integer cId) {
        return cartMapper.selectByPrimaryKey(cId);
    }

    ;

    //
    public int updateCart(Cart cart) {
        //清除缓存
        redisTemplate.delete(CartController.CART_KEY);
        return cartMapper.updateByPrimaryKeySelective(cart);
    }

    public int deleteByCommodityid(String commodityid) {
        redisTemplate.delete(CartController.CART_KEY);
        return cartMapper.deleteByCommodityid(commodityid);
    }

    public void deleteByCommodityids(Set<String> ids) {
        cartMapper.deleteByCommodityids(ids);
    }
}
